package com.liu.blindBox.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.blindBox.entity.qo.BlindBoxInfoQO;
import com.liu.blindBox.entity.vo.BlindBoxInfoVO;
import com.liu.blindBox.entity.vo.BlindBoxTypeInfoVO;
import com.liu.blindBox.entity.vo.SalesSituationVO;
import com.liu.blindBox.mapper.MerchantBlindBoxMapper;
import com.liu.blindBox.service.MerchantBlindBoxService;
import com.liu.common.entity.PicturePath;
import com.liu.common.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MerchantBlindBoxServiceImpl implements MerchantBlindBoxService {

    @Resource
    private MerchantBlindBoxMapper merchantBlindBoxMapper;

    private String picturePath = "";

    @Override
    public int insertProduct(BlindBoxInfoQO blindBoxInfoQO){
        if (this.picturePath.equals("")){
            return 2;
        } else {
            blindBoxInfoQO.setBlindBoxImage(this.picturePath);
        }
        String blindBoxName = merchantBlindBoxMapper.selectBlindBoxNames(blindBoxInfoQO.getBlindBoxName());
        if (blindBoxName != null){
            return 0;
        }
        blindBoxInfoQO.setAverageProfit(blindBoxInfoQO.getMoney().subtract(blindBoxInfoQO.getAverageCost()));
        blindBoxInfoQO.setStatus(0);
        return merchantBlindBoxMapper.insertBlindBoxInfo(blindBoxInfoQO);
    }

    @Override
    public void insertProductImage(MultipartFile image) throws IOException {
        // 图片存储路径
        String path = PicturePath.BLIND_BOX_PICTURE_PATH;
        // 判断是否有路径
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        String fileName = image.getOriginalFilename();
        File imageFile = new File(path,fileName);
        if (!imageFile.exists()) {
            //文件不存在就要新建文件
            imageFile.createNewFile();
        }else {
            //文件存在就要删除文件
            imageFile.delete();
        }
        image.transferTo(imageFile);
        this.picturePath = fileName;
    }

    @Override
    public int updateBlindBoxInfo(BlindBoxInfoQO blindBoxInfoQO) {
        if (!this.picturePath.equals("")){
            blindBoxInfoQO.setBlindBoxImage(this.picturePath);
        }
        blindBoxInfoQO.setAverageProfit(blindBoxInfoQO.getMoney().subtract(blindBoxInfoQO.getAverageCost()));
        blindBoxInfoQO.setStatus(0);
        return merchantBlindBoxMapper.updateBlindBoxInfo(blindBoxInfoQO);
    }

    @Override
    public int deleteBlindBox(String userId, String blindBoxId) {
        return merchantBlindBoxMapper.deleteBlindBox(userId, blindBoxId);
    }

    @Override
    public List<BlindBoxInfoVO> queryBlindBoxInfoByUserId(String userId,String keyword) {
        return merchantBlindBoxMapper.queryBlindBoxInfoByUserId(userId,keyword);
    }

    @Override
    public PageInfo<SalesSituationVO> querySalesSituation(String userId,String keyword,String pageNum,String pageSize) {
        String endTime = DateUtils.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtils.getDateFormat(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd HH:mm:ss");
        PageInfo<SalesSituationVO> salesSituationVOPageInfo = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize)).doSelectPageInfo(() -> {
            merchantBlindBoxMapper.querySalesSituationByUserId(userId, startTime, endTime,keyword);
        });
        // 新建分页
        PageInfo<SalesSituationVO> salesSituationVOPageInfoResult = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(salesSituationVOPageInfo, salesSituationVOPageInfoResult);
        // 对查询的list进行下一步操作
        List<SalesSituationVO> salesSituationVOS = new ArrayList<>(salesSituationVOPageInfoResult.getList());
        salesSituationVOS.forEach(s->{
            switch (s.getBlindBoxStatus()){
                case "0":
                    s.setBlindBoxStatus("上架");
                    break;
                case "1":
                    s.setBlindBoxStatus("下架");
                    break;
            }
        });
        salesSituationVOPageInfoResult.setList(salesSituationVOS);
        return salesSituationVOPageInfoResult;
    }

    @Override
    public List<BlindBoxTypeInfoVO> queryBlindBoxType() {
        return merchantBlindBoxMapper.queryBlindBoxType();
    }

    @Override
    public BlindBoxInfoQO queryBlindBoxInfo(String blindBoxId) {
        return merchantBlindBoxMapper.queryBlindBoxInfo(blindBoxId);
    }
}
